<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/8
 * Time: 18:24
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Coupon extends Base
{
    /**
     * 优惠券列表展示
     * @return mixed|\think\response\Json
     *
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //日期
            if(isset($params['coupon_start']) && $params['coupon_start'] != '' && isset($params['coupon_end']) && $params['coupon_end'] != ''){
                $where['coupon_time'] = ['between', [$params['coupon_start'], $params['coupon_end']]];
            }
            if(isset($params['coupon_start']) && $params['coupon_start'] != '' && isset($params['coupon_end']) && $params['coupon_end'] == ''){
                $where['coupon_time'] = ['>=', $params['coupon_start']];
            }if(isset($params['coupon_start']) && $params['coupon_start'] == '' && isset($params['coupon_end']) && $params['coupon_end'] != ''){
                $where['coupon_time'] = ['<=', $params['coupon_end']];
            }
        
            //学校名
            if(isset($params['coupon_name']) && $params['coupon_name'] != ''){
                $where['coupon_name'] = ['like', '%' . $params['coupon_name'] . '%'];
            }
        
            //类型
            if(isset($params['coupon_use']) && $params['coupon_use'] != ''){
                $where['coupon_use'] = $params['coupon_use'];
            }
            
            $where['coupon_online'] = ['neq', 3];
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('coupon')->where($where)->count();// 获取总条数
            $lists = Db::name('coupon')
                ->field('coupon_id, coupon_time, coupon_name, coupon_price, coupon_use, 
                coupon_end, coupon_online, coupon_start')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('coupon_time desc')
                ->select();
            
            foreach($lists as $key => $v){
                //发放总数
                $total = Db::table('up_my_coupon')
                    ->where('mc_coupon_id', $v['coupon_id'])
                    ->count();
                $lists[$key]['total'] = $total;
                //使用总数
                $use_total = Db::table('up_my_coupon')
                    ->where('mc_coupon_id', $v['coupon_id'])
                    ->where('mc_status', 2)
                    ->count();
                $lists[$key]['use_total'] = $use_total;
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?coupon_id')){
            session('coupon_id', null);
        }
        return $this->fetch();
    }
    
    /**
     * 添加优惠券
     * @return mixed|\think\response\Json
     */
    public function couponAdd(){
        if(Request::instance()->isPost()){
            if(input('coupon_use')!=5&&input('coupon_use')!=6){
                if(input('unbook_id') == '' && input('book_id') == '' && input('unassortment_id') == '' && input('assortment_id') == ''){
                    return json(['code' => 100, 'msg' => '请选择商品/类目']);
                }
            }

            $coupon_good_id = '';
            $assortment_id = '';
            $coupon_is_zd = '';
            $coupon_type = '';
            $coupon_all='';
            //如果只有非图书ID，则存非图书商品
            if(input('unbook_id') != '' && input('book_id') == ''){
                $coupon_is_zd = 2;
                $coupon_good_id = input('unbook_id');
            }
            //如果只有图书ID，则存图书商品
            if(input('unbook_id') == '' && input('book_id') != ''){
                $coupon_is_zd = 2;
                $coupon_good_id = input('book_id');
            }
            //如果只有非图书类目ID，则存非图书类目
            if(input('unassortment_id') != '' && input('assortment_id') == ''){
                $coupon_is_zd = 1;
                $assortment_id = input('unassortment_id');
            }
            //如果只有图书类目ID，则存图书类目
            if(input('unassortment_id') == '' && input('assortment_id') != ''){
                $coupon_is_zd = 1;
                $assortment_id = input('assortment_id');
            }
            //现金
            if(input('coupon_use') == 1 || input('coupon_use') == 3){
                $coupon_type = 1;
            }
            //金币
            if(input('coupon_use') == 2 || input('coupon_use') == 4){
                $coupon_type = 2;
            }
            if(input('coupon_use')==5){
                $coupon_all = 1;
                $coupon_type = 1;
            }
            if(input('coupon_use') ==6){
                $coupon_all= 2;
                $coupon_type = 2;
            }
            $data = [
                'coupon_name' => trim(input('coupon_name'), ' '),
                'coupon_start' => input('coupon_start'),
                'coupon_end' => input('coupon_end'),
                'coupon_price' => input('coupon_price'),
                'coupon_type' => input('coupon_type'),
                'coupon_use' => $coupon_type,
                'coupon_good_id' => $coupon_good_id,
                'coupon_is_zd' => $coupon_is_zd,
                'coupon_assortment' => $assortment_id,
                'coupon_time' => date('Y-m-d H:i:s'),
                'coupon_content' => input('coupon_content'),
                'coupon_num' => input('coupon_num'),
                'coupon_all'=>$coupon_all
            ];
            $res = Db::table('up_coupon')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        //金币
        $books = Db::table('up_goods')
            ->field('good_id, good_name')
            ->where('good_type', 2)
            ->select();
        //现金
        $unbooks = Db::table('up_goods')
            ->field('good_id, good_name')
            ->where('good_type', 1)
            ->select();
        //现金类目
        $money = Db::table('up_goods_assortment')
            ->field('assortment_id, assortment_name')
            ->where('assortment_type', 1)
            ->select();
        //金币类目
        $gold = Db::table('up_goods_assortment')
            ->field('assortment_id, assortment_name')
            ->where('assortment_type', 2)
            ->select();
        
        $this->assign([
            'books' => $books,//   金币/书籍
            'unbooks' => $unbooks,//  现金/非书籍
            'money' => $money,//   现金/非图书
            'gold' => $gold//  金币/图书
        ]);
        return $this->fetch('coupon/couponadd');
    }
    
    /**
     * 批量删除优惠券
     * @return \think\response\Json
     */
    public function couponDel(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择要删除的优惠券']);
        }
        $result = Db::table('up_coupon')->where('coupon_id', 'IN', $ids)->setField('coupon_online', 3);
        if($result){
            return json(['code' => '200', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 批量修改状态
     * @return \think\response\Json
     */
    public function couponStatus(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择优惠券']);
        }
        $status = Db::table('up_coupon')->where('coupon_id', 'IN', $ids)->column('coupon_online');
        if($num == 1){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选优惠券中含有上架中优惠券，请重新选择']);
            }
            $msg = '上架成功';
        }else{//下架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选优惠券中含有已下架优惠券，请重新选择']);
            }
            $msg = '下架成功';
        }
        $res = Db::table('up_coupon')->where('coupon_id', 'IN', $ids)->setField('coupon_online', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 查看获取记录
     * @return mixed|\think\response\Json
     */
    public function couponDetail(){
        if(request()->isAjax ()){
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('my_coupon')
                ->join('up_order', 'order_my_coupon = mc_id', 'left')
                ->where('mc_coupon_id', session('coupon_id'))
                ->count();// 获取总条数
            $lists = Db::name('my_coupon')
                ->join('up_order', 'order_my_coupon = mc_id', 'left')
                ->field('mc_user_id, mc_time, mc_status, order_number, order_user_pay, 
                order_j_glod')
                ->where('mc_coupon_id', session('coupon_id'))
                ->page($Nowpage,$limits)
                ->order('mc_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => session('coupon_id'), 'count' => $count, 'data' => $lists]);
        }
        $coupon_id = input('coupon_id');
        if($coupon_id != ''){
            session('coupon_id', $coupon_id);
        }
        return $this->fetch('coupon/coupondetail');
    }
}